package com.newcapec.modules.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newcapec.common.utils.*;
import com.newcapec.modules.health.dao.StudentDao;
import com.newcapec.modules.health.entity.StudentEntity;
import com.newcapec.modules.health.service.StudentService;
import com.newcapec.modules.sys.entity.SysUserEntity;
import com.newcapec.modules.sys.service.SysConfigService;
import com.newcapec.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;


@Service("studentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysConfigService sysConfigService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        String stuNo = (String)params.get("stuNo");
        String customerId = (String)params.get("customerId");
        String collegeId = (String)params.get("collegeId");
        String majorId = (String)params.get("majorId");
        String classId = (String)params.get("classId");
        String type = (String)params.get("type");
        IPage<StudentEntity> page = this.page(
                new Query<StudentEntity>().getPage(params),
                new QueryWrapper<StudentEntity>()
                        .like(StringUtils.isNotBlank(name),"name", name)
                        .eq(StringUtils.isNotBlank(stuNo),"stu_no", stuNo)
                        .eq(StringUtils.isNotBlank(customerId),"customer_id", customerId)
                        .eq(StringUtils.isNotBlank(collegeId),"college_id", collegeId)
                        .eq(StringUtils.isNotBlank(majorId),"major_id", majorId)
                        .eq(StringUtils.isNotBlank(classId),"class_id", classId)
                        .eq(StringUtils.isNotBlank(type),"feature", type)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(StudentEntity student) {

        String defaultPassword = sysConfigService.getValue(ConfigConstant.DEFAULT_PASSWORD);
        Long roleId = Long.parseLong(sysConfigService.getValue(ConfigConstant.STUDENT_ROLE_ID));

        SysUserEntity user = new SysUserEntity();
        user.setUsername(student.getStuNo());
        user.setPassword(defaultPassword);
        user.setMobile(student.getContacts());
        user.setStatus(1);
        user.setType(Constant.UserType.USER.getValue());
        user.setCreateUserId(ShiroUtils.getUserId());
        user.setRoleIdList(Arrays.asList(roleId));
        sysUserService.saveUser(user);

        student.setUserId(user.getUserId());
        return this.save(student);
    }


}